package io.renren.modules.app.utils;

import java.io.File;
import java.util.HashMap;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.http.HttpUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * https://gitee.com/sjqzhang/go-fastdfs
 * 
 * 文件服务器
 */
@Data
@Slf4j
@RequiredArgsConstructor
public class GoFastdfs {
	/**
	 * 129.204.27.177:8567
	 * 
	 * ip:接口
	 */
	private @NonNull String server;
	/** 场景 */
	private @NonNull String scene;
	private @NonNull String path;

	public Result upload(File file) {
		// 声明参数集合
		HashMap<String, Object> paramMap = new HashMap<>();
		// 文件
		paramMap.put("file", file);
		// 输出
		paramMap.put("output", "json");
		// 自定义路径
		paramMap.put("path", path);
		// 场景
		paramMap.put("scene", scene);
		// 上传
		String result = HttpUtil.post("http://" + server + "/upload", paramMap);
		log.debug("文件上传:{}", result);
		return JSONObject.parseObject(result, Result.class);
	}

	@Data
	@NoArgsConstructor
	public static class Result {
		private Integer retcode;
		private String retmsg;

		private String url;
		private String md5;
		private String path;
		private String domain;
		private String scene;
		private String scenes;
		private String src;
	}

	public static void main(String[] args) {
		// 文件地址
		File file = new File("C:\\Users\\Administrator\\Pictures\\timg.jpg");
		GoFastdfs goFastdfs = new GoFastdfs("129.204.27.177:8567", "pdf", "pdf");
		Result result = goFastdfs.upload(file);
		System.out.println(result);
	}
}
